<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>timer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_timer_timer.js.html">timer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A timer class to which other classes and objects can
<a name="line17"></a> * listen on.  This is only an abstraction above setInterval.
<a name="line18"></a> *
<a name="line19"></a> * @see ../demos/timers.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.Timer&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Class for handling timing events.
<a name="line30"></a> *
<a name="line31"></a> * @param {number=} opt_interval Number of ms between ticks (Default: 1ms).
<a name="line32"></a> * @param {Object=} opt_timerObject  An object that has setTimeout, setInterval,
<a name="line33"></a> *     clearTimeout and clearInterval (eg Window).
<a name="line34"></a> * @constructor
<a name="line35"></a> * @extends {goog.events.EventTarget}
<a name="line36"></a> */
<a name="line37"></a>goog.Timer = function(opt_interval, opt_timerObject) {
<a name="line38"></a>  goog.events.EventTarget.call(this);
<a name="line39"></a>
<a name="line40"></a>  /**
<a name="line41"></a>   * Number of ms between ticks
<a name="line42"></a>   * @type {number}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.interval_ = opt_interval || 1;
<a name="line46"></a>
<a name="line47"></a>  /**
<a name="line48"></a>   * An object that implements setTimout, setInterval, clearTimeout and
<a name="line49"></a>   * clearInterval. We default to the window object. Changing this on
<a name="line50"></a>   * goog.Timer.prototype changes the object for all timer instances which can
<a name="line51"></a>   * be useful if your environment has some other implementation of timers than
<a name="line52"></a>   * the window object.
<a name="line53"></a>   * @type {Object}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.timerObject_ = opt_timerObject || goog.Timer.defaultTimerObject;
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * Cached tick_ bound to the object for later use in the timer.
<a name="line60"></a>   * @type {Function}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.boundTick_ = goog.bind(this.tick_, this);
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * Firefox browser often fires the timer event sooner
<a name="line67"></a>   * (sometimes MUCH sooner) than the requested timeout. So we
<a name="line68"></a>   * compare the time to when the event was last fired, and
<a name="line69"></a>   * reschedule if appropriate. See also goog.Timer.intervalScale
<a name="line70"></a>   * @type {number}
<a name="line71"></a>   * @private
<a name="line72"></a>   */
<a name="line73"></a>  this.last_ = goog.now();
<a name="line74"></a>};
<a name="line75"></a>goog.inherits(goog.Timer, goog.events.EventTarget);
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Maximum timeout value.
<a name="line80"></a> *
<a name="line81"></a> * Timeout values too big to fit into a signed 32-bit integer may cause
<a name="line82"></a> * overflow in FF, Safari, and Chrome, resulting in the timeout being
<a name="line83"></a> * scheduled immediately.  It makes more sense simply not to schedule these
<a name="line84"></a> * timeouts, since 24.8 days is beyond a reasonable expectation for the
<a name="line85"></a> * browser to stay open.
<a name="line86"></a> *
<a name="line87"></a> * @type {number}
<a name="line88"></a> * @private
<a name="line89"></a> */
<a name="line90"></a>goog.Timer.MAX_TIMEOUT_ = 2147483647;
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Whether this timer is enabled
<a name="line95"></a> * @type {boolean}
<a name="line96"></a> */
<a name="line97"></a>goog.Timer.prototype.enabled = false;
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * An object that implements setTimout, setInterval, clearTimeout and
<a name="line102"></a> * clearInterval. We default to the global object. Changing
<a name="line103"></a> * goog.Timer.defaultTimerObject changes the object for all timer instances
<a name="line104"></a> * which can be useful if your environment has some other implementation of
<a name="line105"></a> * timers you&#39;d like to use.
<a name="line106"></a> * @type {Object}
<a name="line107"></a> */
<a name="line108"></a>goog.Timer.defaultTimerObject = goog.global;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * A variable that controls the timer error correction. If the
<a name="line113"></a> * timer is called before the requested interval times
<a name="line114"></a> * intervalScale, which often happens on mozilla, the timer is
<a name="line115"></a> * rescheduled. See also this.last_
<a name="line116"></a> * @type {number}
<a name="line117"></a> */
<a name="line118"></a>goog.Timer.intervalScale = 0.8;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Variable for storing the result of setInterval
<a name="line123"></a> * @type {?number}
<a name="line124"></a> * @private
<a name="line125"></a> */
<a name="line126"></a>goog.Timer.prototype.timer_ = null;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Gets the interval of the timer.
<a name="line131"></a> * @return {number} interval Number of ms between ticks.
<a name="line132"></a> */
<a name="line133"></a>goog.Timer.prototype.getInterval = function() {
<a name="line134"></a>  return this.interval_;
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Sets the interval of the timer.
<a name="line140"></a> * @param {number} interval Number of ms between ticks.
<a name="line141"></a> */
<a name="line142"></a>goog.Timer.prototype.setInterval = function(interval) {
<a name="line143"></a>  this.interval_ = interval;
<a name="line144"></a>  if (this.timer_ &amp;&amp; this.enabled) {
<a name="line145"></a>    // Stop and then start the timer to reset the interval.
<a name="line146"></a>    this.stop();
<a name="line147"></a>    this.start();
<a name="line148"></a>  } else if (this.timer_) {
<a name="line149"></a>    this.stop();
<a name="line150"></a>  }
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Callback for the setTimeout used by the timer
<a name="line156"></a> * @private
<a name="line157"></a> */
<a name="line158"></a>goog.Timer.prototype.tick_ = function() {
<a name="line159"></a>  if (this.enabled) {
<a name="line160"></a>    var elapsed = goog.now() - this.last_;
<a name="line161"></a>    if (elapsed &gt; 0 &amp;&amp;
<a name="line162"></a>        elapsed &lt; this.interval_ * goog.Timer.intervalScale) {
<a name="line163"></a>      this.timer_ = this.timerObject_.setTimeout(this.boundTick_,
<a name="line164"></a>          this.interval_ - elapsed);
<a name="line165"></a>      return;
<a name="line166"></a>    }
<a name="line167"></a>
<a name="line168"></a>    this.dispatchTick();
<a name="line169"></a>    // The timer could be stopped in the timer event handler.
<a name="line170"></a>    if (this.enabled) {
<a name="line171"></a>      this.timer_ = this.timerObject_.setTimeout(this.boundTick_,
<a name="line172"></a>          this.interval_);
<a name="line173"></a>      this.last_ = goog.now();
<a name="line174"></a>    }
<a name="line175"></a>  }
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Dispatches the TICK event. This is its own method so subclasses can override.
<a name="line181"></a> */
<a name="line182"></a>goog.Timer.prototype.dispatchTick = function() {
<a name="line183"></a>  this.dispatchEvent(goog.Timer.TICK);
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Starts the timer.
<a name="line189"></a> */
<a name="line190"></a>goog.Timer.prototype.start = function() {
<a name="line191"></a>  this.enabled = true;
<a name="line192"></a>
<a name="line193"></a>  // If there is no interval already registered, start it now
<a name="line194"></a>  if (!this.timer_) {
<a name="line195"></a>    // IMPORTANT!
<a name="line196"></a>    // window.setInterval in FireFox has a bug - it fires based on
<a name="line197"></a>    // absolute time, rather than on relative time. What this means
<a name="line198"></a>    // is that if a computer is sleeping/hibernating for 24 hours
<a name="line199"></a>    // and the timer interval was configured to fire every 1000ms,
<a name="line200"></a>    // then after the PC wakes up the timer will fire, in rapid
<a name="line201"></a>    // succession, 3600*24 times.
<a name="line202"></a>    // This bug is described here and is already fixed, but it will
<a name="line203"></a>    // take time to propagate, so for now I am switching this over
<a name="line204"></a>    // to setTimeout logic.
<a name="line205"></a>    //     https://bugzilla.mozilla.org/show_bug.cgi?id=376643
<a name="line206"></a>    //
<a name="line207"></a>    this.timer_ = this.timerObject_.setTimeout(this.boundTick_,
<a name="line208"></a>        this.interval_);
<a name="line209"></a>    this.last_ = goog.now();
<a name="line210"></a>  }
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Stops the timer.
<a name="line216"></a> */
<a name="line217"></a>goog.Timer.prototype.stop = function() {
<a name="line218"></a>  this.enabled = false;
<a name="line219"></a>  if (this.timer_) {
<a name="line220"></a>    this.timerObject_.clearTimeout(this.timer_);
<a name="line221"></a>    this.timer_ = null;
<a name="line222"></a>  }
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/** @override */
<a name="line227"></a>goog.Timer.prototype.disposeInternal = function() {
<a name="line228"></a>  goog.Timer.superClass_.disposeInternal.call(this);
<a name="line229"></a>  this.stop();
<a name="line230"></a>  delete this.timerObject_;
<a name="line231"></a>};
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Constant for the timer&#39;s event type
<a name="line236"></a> * @type {string}
<a name="line237"></a> */
<a name="line238"></a>goog.Timer.TICK = &#39;tick&#39;;
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>/**
<a name="line242"></a> * Calls the given function once, after the optional pause.
<a name="line243"></a> *
<a name="line244"></a> * The function is always called asynchronously, even if the delay is 0. This
<a name="line245"></a> * is a common trick to schedule a function to run after a batch of browser
<a name="line246"></a> * event processing.
<a name="line247"></a> *
<a name="line248"></a> * @param {Function} listener Function or object that has a handleEvent method.
<a name="line249"></a> * @param {number=} opt_delay Milliseconds to wait; default is 0.
<a name="line250"></a> * @param {Object=} opt_handler Object in whose scope to call the listener.
<a name="line251"></a> * @return {number} A handle to the timer ID.
<a name="line252"></a> */
<a name="line253"></a>goog.Timer.callOnce = function(listener, opt_delay, opt_handler) {
<a name="line254"></a>  if (goog.isFunction(listener)) {
<a name="line255"></a>    if (opt_handler) {
<a name="line256"></a>      listener = goog.bind(listener, opt_handler);
<a name="line257"></a>    }
<a name="line258"></a>  } else if (listener &amp;&amp; typeof listener.handleEvent == &#39;function&#39;) {
<a name="line259"></a>    // using typeof to prevent strict js warning
<a name="line260"></a>    listener = goog.bind(listener.handleEvent, listener);
<a name="line261"></a>  } else {
<a name="line262"></a>    throw Error(&#39;Invalid listener argument&#39;);
<a name="line263"></a>  }
<a name="line264"></a>
<a name="line265"></a>  if (opt_delay &gt; goog.Timer.MAX_TIMEOUT_) {
<a name="line266"></a>    // Timeouts greater than MAX_INT return immediately due to integer
<a name="line267"></a>    // overflow in many browsers.  Since MAX_INT is 24.8 days, just don&#39;t
<a name="line268"></a>    // schedule anything at all.
<a name="line269"></a>    return -1;
<a name="line270"></a>  } else {
<a name="line271"></a>    return goog.Timer.defaultTimerObject.setTimeout(
<a name="line272"></a>        listener, opt_delay || 0);
<a name="line273"></a>  }
<a name="line274"></a>};
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/**
<a name="line278"></a> * Clears a timeout initiated by callOnce
<a name="line279"></a> * @param {?number} timerId a timer ID.
<a name="line280"></a> */
<a name="line281"></a>goog.Timer.clear = function(timerId) {
<a name="line282"></a>  goog.Timer.defaultTimerObject.clearTimeout(timerId);
<a name="line283"></a>};
</pre>


</body>
</html>
